﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockManagement.Model;
using StockManagement.Helper;
using System.Collections.ObjectModel;

namespace StockManagement.ViewModels
{
    public class ProductViewModel : BaseViewModel
    {
        Product _product;        

        public ProductViewModel()
        {
            _product = new Product();
        }

        public ProductViewModel(Product product)
        {
            _product = product;
        }

        public Guid ProductID
        {
            get { return _product.ProductID; }
        }

        public string ProductName
        {
            get { return _product.Name; }
            set
            {
                _product.Name = value;
                OnPropertyChanged("ProductName");
            }
        }

        public EnumMaterialType ProductType
        {
            get { return _product.ProductType; }
            set
            {
                _product.ProductType = value;
                OnPropertyChanged("ProductType");
            }
        }

        public ProductStatus ProductStatus
        {
            get { return _product.Status; }
            set
            {
                _product.Status = value;
                OnPropertyChanged("ProductStatus");
            }
        }

        public string ProductCode
        {
            get { return _product.ProductCode; }
            set
            {
                _product.ProductCode = value;
                OnPropertyChanged("ProductCode");
            }
        }

        public ProductMaterialList MaterialsQuantity
        {
            get { return _product.MaterialsQuantity; }
            set { _product.MaterialsQuantity = value;}
        }

        public string Purchaser
        {
            get { return _product.Purchaser; }
            set 
            {
                _product.Purchaser = value;
                OnPropertyChanged("Purchaser");
            }
        }

        public DateTime Date
        {
            get { return _product.Date; }
            set
            { 
                _product.Date = value;
                OnPropertyChanged("Date");
            }
        }

        public string Consumer
        {
            get { return _product.Consumer; }
            set
            {
                _product.Consumer = value;
                OnPropertyChanged("Consumer");
            }
        }

        internal bool DiscardProduct()
        {
            _product = new Product();
            OnPropertyChanged("ProductName");
            OnPropertyChanged("ProductType");
            OnPropertyChanged("ProductCode");

            OnPropertyChanged("ProductStatus");
            OnPropertyChanged("MaterialsQuantity");
            OnPropertyChanged("Purchaser");
            OnPropertyChanged("Date");
            OnPropertyChanged("Consumer");
            OnPropertyChanged("Date");
            OnPropertyChanged("Date");
            return true;
        }

        internal bool SaveProduct()
        {
            return Adapter.AddNewProduct(_product);
        }

        internal bool DeleteProduct()
        {
            return Adapter.DeleteProduct(_product.ProductID);
        }

        internal bool UpdateProduct(UpdateType updateType, string prodName = "", 
            ProductMaterialList lstProdMaterial = null, string consumer="", string purchaser="")
        {
            bool vaild = false;
            switch (updateType)
            {
                case UpdateType.Edit:
                    vaild = Adapter.UpdateEditProduct(_product, prodName, lstProdMaterial);
                    break;
                case UpdateType.Consume:
                    vaild = Adapter.UpdateConsumeProduct(_product, consumer);
                    break;
                case UpdateType.ConsumeComplete:
                    vaild = Adapter.UpdateConsumeCompleteProduct(_product);
                    break;
                case UpdateType.Deliver:
                    vaild = Adapter.UpdateDeliverProduct(_product, purchaser);
                    break;
            }
            return vaild;
        }

        internal void AddProductMaterialQuantity(ProductMaterial prodMaterial)
        {           
            _product.MaterialsQuantity.Add(new ProductMaterial(prodMaterial.MaterialID,prodMaterial.MaterialName,prodMaterial.MaterialCode)
            {
                MaterialQuantity = prodMaterial.MaterialQuantity,
                IsSelected = true
            });
        }

        internal void UpdateProductMaterialQuantity(ProductMaterial prodMaterial)
        {
            var prodMat = _product.MaterialsQuantity.Find(p => p.MaterialID == prodMaterial.MaterialID);
            if (prodMat == null)
                AddProductMaterialQuantity(prodMaterial);
            else
                prodMat.MaterialQuantity = prodMaterial.MaterialQuantity;
        }
    }
}
